package com.kylin.core.merchant.query.handler;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.kylin.dal.entity.commodity.CommentEntry;
import com.kylin.order.api.dto.resp.ConfirmOrderRespDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 订单秒结修复防腐层实现类
 * @author Damon S.
 */
@Slf4j
@Service
public final class RealtimeOrderingTranslator {

    private static final ObjectMapper JSON_MAPPER = new ObjectMapper();

    public ConfirmOrderRespDTO translateToRespDTO(CommentEntry commentEntry) {
        ConfirmOrderRespDTO appInfoRespDTO = new ConfirmOrderRespDTO();
        BeanUtils.copyProperties(commentEntry, appInfoRespDTO);
        return appInfoRespDTO;
    }

    public List<ConfirmOrderRespDTO> translateToRespDTO(List<CommentEntry> entries) {
        List<ConfirmOrderRespDTO> appInfoRespDTOs = new ArrayList<>(entries.size());
        entries.forEach(
                entry -> appInfoRespDTOs.add(translateToRespDTO(entry))
        );
        return appInfoRespDTOs;
    }

//    public List<OrderTobeSettled> translateToOrderTobeSettled(List<GalleryEntry> entries) {
//        return Optional.ofNullable(entries)
//                .orElse(Collections.emptyList())
//                .stream()
//                .map(this::toOrderTobeSettled)
//                .collect(Collectors.toCollection(LinkedList::new));
//    }
//
//
//    public List<SettlementPolicyTemplate> translateToListFrom(List<GroupCommodityEntry> entries) {
//        return entries.stream()
//                .map(entry -> SettlementPolicyTemplate.builder()
//                        .policyId(entry.getPolicyId())
//                        .platform(Platform.valueOf(entry.getPlatform().toUpperCase()))
//                        .policyName(entry.getPolicyName())
//                        .policyType(PolicyType.valueOf(entry.getPolicyType().toUpperCase()))
//                        .bonusCode(ApprovalState.valueOf(entry.getBonusCode()))
//                        .bonusType(MarketingType.parse(entry.getBonusType()))
//                        .bonusAmount(entry.getBonusAmount())
//                        .bonusSource(GrouponState.valueOf(entry.getBonusSource()))
//                        .buyerType(entry.getBuyerType())
//                        .buyerSubtype(entry.getBuyerSubtype())
//                        .position(entry.getPosition())
//                        .joinType(entry.getJoinType())
//                        .cutoffType(entry.getCutOff())
//                        .beneficiary(RewardSubject.valueOf(entry.getBeneficiary()))
//                        .beneficiaryType(entry.getBeneficiaryType())
//                        .beneficiarySubtype(entry.getBeneficiarySubtype())
//                        .removed(entry.getRemoved())
//                        .ext(parse(entry.getExt())).build())
//                .collect(Collectors.toCollection(LinkedList::new));
//    }
//
//    public List<FixOrderItem> translateToListFrom(QueryCommodityReqDTO queryCommodityReqDTO) {
//        if (Objects.isNull(queryCommodityReqDTO)) {
//            return Collections.emptyList();
//        }
//        return Optional.ofNullable(queryCommodityReqDTO.getOrders())
//                .orElse(Collections.emptyList())
//                .stream()
//                .map(req -> FixOrderItem.builder()
//                        .wrongJoinType(JoinType.parse(req.getWrongJoinType()))
//                        .subOrderId(req.getOrderId())
//                        .build()
//                ).collect(Collectors.toCollection(LinkedList::new));
//    }
//
//
//    private SettlementPolicyTemplate.ExtParams parse(String ext) {
//        try {
//            if (!StringUtils.isEmpty(ext)) {
//                return JSON_MAPPER.readValue(ext, SettlementPolicyTemplate.ExtParams.class);
//            }
//        } catch (Exception e) {
//            log.error("结算规则数据[SETTLEMENT_RULE]扩展项转换JSON对象失败。ext:{}", ext);
//            throw new SystemException(e);
//        }
//        return null;
//    }
//
//    public OrderTobeSettled toOrderTobeSettled(GalleryEntry entry) {
//        OrderTobeSettled.OrderTobeSettledBuilder builder = OrderTobeSettled.builder()
//                .id(entry.getId())
//                .subOrderNo(entry.getOrderNo())
//                .channel(Platform.parse(entry.getChannel()))
//                .buyerCpId(entry.getBuyerCpId())
//                .fromCpId(entry.getFromCpId())
//                .groupCpId(entry.getGroupCpId())
//                .joinType(JoinType.parse(entry.getJoinType()))
//                .itemCount(entry.getItemSum())
//                .detailCount(entry.getDetailCount().intValue())
//                .activated(YesNoEnum.parse(entry.getActivated()))
//                .promoAmount(entry.getPromoAmount())
//                .savingAmount(entry.getSavingAmount())
//                .serveAmount(entry.getServeAmount())
//                .freshmanRewarded(YesNoEnum.parse(entry.getFreshmanRewarded().intValue()))
//                .activated(YesNoEnum.parse(entry.getActivated()))
//                .reverseState(ReverseState.FORWARDING)
//                .vp(entry.getVp())
//                .productAmount(entry.getProductAmount())
//                .orderMonth(entry.getOrderMonth())
//                .commissionProductAmount(entry.getCommissionProductAmount())
//                .supplemented(YesNoEnum.parse(entry.getSupplemented()))
//                .subChannel(entry.getSubChannel())
//                .sourceId(entry.getSourceId())
//                .removed(YesNoEnum.parse(entry.getRemoved()))
//                .createdAt(entry.getCreatedAt().toLocalDateTime())
//                .buyer(UserInSettlement.EMPTY)
//                .fromUser(UserInSettlement.EMPTY)
//                .groupUser(UserInSettlement.EMPTY);
//
//        Optional.ofNullable(entry.getCommitPaidAt()).ifPresent(
//                c -> builder.commitPaidAt(c.toLocalDateTime()));
//        Optional.ofNullable(entry.getFinishPaidAt()).ifPresent(
//                f -> builder.finishPaidAt(f.toLocalDateTime()));
////        if (JoinType.DOWNGRADE.getCode().equals(entry.getJoinType())) {
////            builder.joinType(JoinType.VIP);
////            builder.reverseState(ReverseState.REVERSING);
////        }
//        return builder.build();
//    }
}